#include "Test.Application.h"

#undef  APP_NAME
#define APP_NAME	TestTexturedBatch
#undef  APP_BASE
#define APP_BASE	TestApp


class APP_NAME : public APP_BASE
{
protected:
	BatchMaterial	material;


public:
	APP_NAME (IPlatform *platform, EDevice::type deviceType) :
		APP_BASE( platform, deviceType )
	{}

	void Update (float dt) override
	{
		TestApp::Update( dt );

		ESS()->GetGraphicsEngine()->GetViewport()->Apply();
		ESS()->GetGLStateManager()->ClearColor( color4f( 0.0f, 0.0f, 1.0f, 1.0f ) );

		batch.AddBatch( material, Rect2D( rect(-0.95f, -0.95f, 0.95f, 0.95f), rect(0.0f, 0.0f, 1.0f, 1.0f), color4u(255, 255, 255, 255) ) );

		batch.Flush();
		batch.Clear();
		
		ESS()->GetGraphicsEngine()->SwapBuffers();
	}

	void OnEnter () override
	{
		APP_BASE::OnEnter();

		TexturePtr	texture;
		resMngr.Load( EResource::TEXTURE, "tex/play", texture );

		ProgramPtr	program = ESS()->GetGraphicsEngine()->GetShaderManager()->
								GetDefaultProgram( EDefaultShader::COLORED_BLEND_WITH_TEXTURE );

		material.SetTexture( 0, texture );
		material.SetProgram( program );
		
		RenderState::ColorState	cs;
		cs.blend		= true;
		cs.blendFuncSrc	= gl_blend_func::SRC_ALPHA;
		cs.blendFuncDst	= gl_blend_func::ONE_MINUS_SRC_ALPHA;

		//material.SetColorState( cs );

		static const uint8	image_data[] = {
			255, 0, 0,	255, 0, 0,	255, 0, 0,	255, 0, 0,
			255, 0, 0,	255, 0, 0,	255, 0, 0,	255, 0, 0,
			255, 0, 0,	255, 0, 0,	255, 0, 0,	255, 0, 0,
			255, 0, 0,	255, 0, 0,	255, 0, 0,	255, 0, 0
		};

		//texture->AddImage( uvec3(16, 16, 0), uvec3(4,4,0), EPixelFormat::RGB8, 0, uni_binbuffer_t( image_data ) );
	}
	
	void OnExit () override
	{
		material.Clear();
		APP_BASE::OnExit();
	}
};